package com.demo.controller;

import com.demo.entity.Message;
import com.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;


@Controller					//加入IOC容器
@RequestMapping("controller")
public class MessageController {
	//控制器依赖于Service
	@Autowired
	@Qualifier("messageService")
	private MessageService messageService;
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	//插入留言信息
	@RequestMapping("insertMessageInfo/{pid}")
	public String insertMessageInfo(@PathVariable("pid")String pid,HttpServletRequest req) throws UnsupportedEncodingException{
		String uname = (String) req.getSession().getAttribute("USERNAME");			//用户名
		String pageMsg = new String(req.getParameter("pageMsg").getBytes("iso-8859-1"),"UTF-8");		//留言信息

		messageService.insertMessageInfo(pid, uname, pageMsg);
		System.out.println("留言成功.................");
		return "redirect:/controller/detailCartitemInfo/"+pid;
	}

	//查询所有留言信息
	@RequestMapping("message_info_list_action")
	public String selectAllMessageList(HttpServletRequest req){
		ArrayList<Message> message = messageService.selectAllMessage();
		System.out.println("查找所有留言信息...................");
		req.getSession().setAttribute("messageList", message);
		return "messageList";
	}
	//删除留言信息
	@RequestMapping("deleteMessageInfomation/{content}")
	public String deleteMessageInfo(@PathVariable("content")String content,HttpServletRequest req){
		messageService.deleteMessageInfo(content);
		System.out.println("删除cg..........................");
		return "redirect:/controller/message_info_list_action";
	}

}